const process = require("child_process");
const os = require("os");
const fs = require("fs");
const homedir = os.homedir();
const fileName = "today-log.log";
const filePath = `${homedir}\\Desktop\\${fileName}`;
const exportDate = new Date();
const prevTime = `${exportDate.getFullYear()}-${exportDate.getMonth() +
  1}-${exportDate.getDate()} 00:00:00`;
if (fs.existsSync(filePath)) {
  fs.unlinkSync(filePath);
}
process.exec("git config user.name", (error, stdout, stderr) => {
  const author = stdout.replace(/\n/g, "");
  const cmd = `git log --pretty=medium --after="${prevTime}" --author="${author}" >> ${filePath}`;
  process.exec(cmd, () => {
    const text = fs.readFileSync(filePath, "utf8");
    const resultText = text
      .replace(/commit.+/g, "*")
      .replace(/Author:.+/g, "")
      .replace(/Date:.+/g, "")
      .replace(/\s{10}/g, "：")
      .replace(/: /g, "-")
      .replace(/\s{8}/g, " ")
      .replace(/\* /g, "\n*");
    fs.writeFile(filePath, resultText, "utf8", function() {
      console.log("Git日志-生成成功！");
    });
  });
});
